package com.example.service.serviceImpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.CaptureConfig;
import com.example.common.JwtTokenUtils;
import com.example.common.UserHolder;
import com.example.domain.entity.*;
import com.example.exception.CustomException;
import com.example.mapper.EmpMapper;
import com.example.service.EmpService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

//    @Resource
//    private TeacherDao teacherDao;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> findAll() {
        return empMapper.selectList(null);
    }

    @Override
    public Page<Emp> findBySearch(Params params) {
        // 创建 MyBatis-Plus 的分页对象
        Page<Emp> page = new Page<>(params.getPageNum(), params.getPageSize());

        // 创建查询条件构造器
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();

        // 根据 params 构建查询条件
        if (params != null) {
            if (params.getName() != null && !params.getName().isEmpty()) {
                queryWrapper.like("name", params.getName());
            }
            if (params.getPhone() != null && !params.getPhone().isEmpty()) {
                queryWrapper.like("phone", params.getPhone());
            }
        }

        // 按 id 倒序排序
        queryWrapper.orderByDesc("id");

        // 执行分页查询
        return empMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void add(Emp emp) {
        if (emp.getUsername() == null || "".equals(emp.getUsername())) {
            throw new CustomException("用户名不能为空");
        }
        Emp user = empMapper.findByUserName(emp.getUsername());
        if (user != null) {
            throw new CustomException("该用户名已存在，请更换用户名");
        }
        if (emp.getPassword() == null) {
            emp.setPassword("123456");
        }
        emp.setRole("TEACHER");
        empMapper.insert(emp);
    }

    @Override
    public void update(Emp emp) {
        empMapper.updateById(emp);
    }

    @Override
    public void delete(Integer id) {
        empMapper.deleteById(id);
    }

    @Override
    public Account login(Account teacher, String key, HttpServletRequest request) {
        if (!teacher.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))) {
            CaptchaUtil.clear(request);
            throw new CustomException("验证码不正确");
        }
        CaptureConfig.CAPTURE_MAP.remove(key);
        if (teacher.getUsername() == null || "".equals(teacher.getUsername())) {
            throw new CustomException("用户名不能为空");
        }
        if (teacher.getPassword() == null || "".equals(teacher.getPassword())) {
            throw new CustomException("密码不能为空");
        }
        Emp user = empMapper.findByNameAndPassword(teacher.getUsername(), teacher.getPassword());
        if (user == null) {
            throw new CustomException("用户名或密码输入错误");
        }
        if (user.getStatus() == 1) {
            throw new CustomException("该用户已被禁用");
        }
        String tokenData = user.getId() + "-" + user.getRole();
        String token = JwtTokenUtils.genToken(tokenData, user.getPassword());
        user.setToken(token);
        return user;
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    public void updatePassword(Account account) {
        Emp emp = empMapper.findByUserName(account.getUsername());
        if(ObjectUtil.isNull(emp)){
            throw new CustomException("用户不存在");
        }
        if(!account.getPassword().equals(emp.getPassword())){
            throw new CustomException("原密码不正确");
        }
        emp.setPassword(account.getNewPassword());
        empMapper.updateById(emp);
        UserHolder.removeUser();
    }

    @Override
    public void updatePersonal(Account account) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(account, emp);
        update(emp);
    }
}